package com.hongxun.design.activitys;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.hongxun.design.MyApp;
import com.hongxun.design.R;
import com.hongxun.design.acceptance.fragments.ProjectAcceptFragment;
import com.hongxun.design.constant.Constant;
import com.hongxun.design.fragments.BaseFragment;
import com.hongxun.design.fragments.ProjectDetailsFragment;
import com.hongxun.design.interfaces.HeadSecond;
import com.hongxun.design.listeners.ListenerSet;
import com.hongxun.design.mytools.IconSourse;
import com.hongxun.design.mytools.IntentToActivity;
import com.hongxun.design.mytools.MString;
import com.hongxun.design.mytools.ToastShow;
import com.hongxun.design.ui.headandbottonpanel.HeadSecondControlPanel;
import com.hongxun.design.utils.DataUtil;


/**
 * Created by zerdoor_pc on 2016/6/5.
 */
public class ProjectDetailsActivity extends BaseActivity implements HeadSecond, View.OnClickListener {
    public String titile = "";
    public HeadSecondControlPanel bit_layout;
    //选项卡
    LinearLayout tab1, tab2, tab3, tab4, tab5, tab6;
    TextView basicinformation, columndetail, crossing, materialstatistics, electrichousehold, problems;
    private int index = 2;
    // fragment管理类
    private FragmentManager fragmentManager;
    //fragment
    private ProjectDetailsFragment tab1Fragment = new ProjectDetailsFragment();
    private ProjectDetailsFragment tab2Fragment = new ProjectDetailsFragment();
    private ProjectDetailsFragment tab3Fragment = new ProjectDetailsFragment();
    private ProjectDetailsFragment tab4Fragment = new ProjectDetailsFragment();
    private ProjectDetailsFragment tab5Fragment = new ProjectDetailsFragment();
    private ProjectAcceptFragment tab6Fragment = new ProjectAcceptFragment();
    private String texttype, explor_id, pointType;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_projectdetails);
        findBar();
        findView();
        initialization();
    }

    public void getData() {
        titile = ProjectMapActivity.projectName;
        Bundle bundle = getIntent().getExtras();
        try {

            texttype = bundle.getString(MString.TextTYPE);
            pointType = bundle.getString(MString.exploration_type);
            explor_id = bundle.getString(MString.explor_id);
            if (texttype.equals(MString.code_one)) {
                titile = bundle.getString(MString.number);
            }
        } catch (Exception e) {
        }
    }

    @Override
    public void findBar() {
        getData();
        bit_layout = (HeadSecondControlPanel) findViewById(R.id.details_layout);
        bit_layout.initHeadPanel(DataUtil.getsecondbean("编辑", View.INVISIBLE, R.color.bar_right, ListenerSet.back, titile, R.color.title, View.VISIBLE, IconSourse.BACK + "", View.VISIBLE));
        if (MyApp.ReadOnly) {
            bit_layout.mRightTitle.setVisibility(View.GONE);
        } else {
            bit_layout.setVisibility(View.VISIBLE);
            bit_layout.setClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    switch (index) {
                        case Constant.basicinformation://编辑
                            if (MyApp.ReadOnly) {
                                ToastShow.showShort(MyApp.activity, "没有编辑权限！");
                                return;
                            }
                            if (MyApp.currProject.getManager_name().contains(MyApp.userInfo.getUser_id())
                                    || MyApp.currProject.getManager().contains(MyApp.userInfo.getUser_id())) {
                                MyApp.isupdate = true;
                                Bundle bundle = new Bundle();
                                bundle.putString(MString.TYPE, MString.ISEDIT);
                                IntentToActivity.intent(MyApp.activity, CreateProjectActivity.class, bundle);
                            } else {
                                ToastShow.showShort(MyApp.activity, "没有编辑权限！");
                                return;
                            }
                            break;
                        case Constant.problems://导出
                            // TODO: 导出问题汇总
                            break;
                    }
                }
            });
        }

    }

    @Override
    protected void onResume() {
        MyApp.activity = this;
        mActivity = this;
        super.onResume();
    }


    @Override
    protected void findView() {
        super.findView();
        fragmentManager = getSupportFragmentManager();
        /**基础信息**/
        tab1 = (LinearLayout) findViewById(R.id.button_basicinformation);
        basicinformation = (TextView) findViewById(R.id.basicinformation);
        /**塔杆明细**/
        tab2 = (LinearLayout) findViewById(R.id.button_columndetail);
        columndetail = (TextView) findViewById(R.id.columndetail);
        /**交叉跨越**/
        tab3 = (LinearLayout) findViewById(R.id.button_crossing);
        crossing = (TextView) findViewById(R.id.crossing);
        /**材料统计**/
        tab4 = (LinearLayout) findViewById(R.id.button_materialstatistics);
        materialstatistics = (TextView) findViewById(R.id.materialstatistics);
        /**用电户**/
        tab5 = (LinearLayout) findViewById(R.id.button_electrichousehold);
        electrichousehold = (TextView) findViewById(R.id.electrichousehold);
        /**问题汇总**/
        tab6 = (LinearLayout) findViewById(R.id.button_Problems);
        // TODO: 验收记录-问题汇总
        tab6.setVisibility(View.VISIBLE);
        problems = (TextView) findViewById(R.id.problems);
        //checkChange(tab1Fragment, tab1, basicinformation, Constant.basicinformation);
        tab1.setOnClickListener(this);
        tab2.setOnClickListener(this);
        tab3.setOnClickListener(this);
        tab4.setOnClickListener(this);
        tab5.setOnClickListener(this);
        tab6.setOnClickListener(this);
    }

    @Override
    protected void initialization() {
        super.initialization();
        onClick(tab1);
    }

    @Override
    public void onClick(View v) {
        bit_layout.mRightTitle.setVisibility(View.INVISIBLE);
        switch (v.getId()) {
            case R.id.button_basicinformation://基础信息
                if (MyApp.ReadOnly) {
                    bit_layout.mRightTitle.setVisibility(View.GONE);
                } else {
                    bit_layout.mRightTitle.setText("编辑");
                    bit_layout.mRightTitle.setVisibility(View.VISIBLE);
                }
                checkChange(tab1Fragment, tab1, basicinformation, Constant.basicinformation);
                break;
            case R.id.button_columndetail://塔杆明细
                checkChange(tab2Fragment, tab2, columndetail, Constant.columndetail);
                break;
            case R.id.button_crossing://交叉跨越
                checkChange(tab3Fragment, tab3, crossing, Constant.crossing);
                break;
            case R.id.button_materialstatistics://材料统计
                checkChange(tab4Fragment, tab4, materialstatistics, Constant.materialstatistics);
                break;
            case R.id.button_electrichousehold://用电户
                checkChange(tab5Fragment, tab5, electrichousehold, Constant.electrichousehold);
                break;
            case R.id.button_Problems://问题汇总
                bit_layout.mRightTitle.setVisibility(View.VISIBLE);
                bit_layout.mRightTitle.setText("导出");
                checkChange(tab6Fragment, tab6, problems, Constant.problems);
                break;
        }


    }


    /**
     * 切换*
     */
    private void checkChange(BaseFragment f, View v, TextView words, int i) {
        if (index == i) return;
        clearStatus();
//        if (f == null) {
//            f = new ProjectDetailsFragment();
//        }
        replaceFragment(f, i);
        v.setBackgroundResource(R.drawable.wihte_item);
        words.setTextColor(getResources().getColor(R.color.caikan_bule));
        index = i;
    }

    /**
     * 切换fragment
     *
     * @param newFragment
     */
    Fragment currtFragment;

    private void replaceFragment(Fragment newFragment, int i) {
        FragmentTransaction transaction = fragmentManager.beginTransaction();
        if (!newFragment.isAdded()) {
            Bundle bundle = new Bundle();
            bundle.putInt(MString.TYPE, i);
            bundle.putString(MString.TextTYPE, texttype);
            bundle.putString(MString.explor_id, explor_id);
            bundle.putString(MString.exploration_type, pointType);
            newFragment.setArguments(bundle);
            transaction.add(R.id.details_layout_frame, newFragment);
//            switch (i) {
//                case Constant.basicinformation:
//                    tab1Fragment = (ProjectDetailsFragment) newFragment;
//                    break;
//                case Constant.columndetail:
//                    tab2Fragment = (ProjectDetailsFragment) newFragment;
//                    break;
//                case Constant.crossing:
//                    tab3Fragment = (ProjectDetailsFragment) newFragment;
//                    break;
//                case Constant.materialstatistics:
//                    tab4Fragment = (ProjectDetailsFragment) newFragment;
//                    break;
//                case Constant.electrichousehold:
//                    tab5Fragment = (ProjectDetailsFragment) newFragment;
//                    break;
//            }

        } else {
            transaction.show(newFragment);
        }
        if (currtFragment != null && currtFragment != newFragment) {
            transaction.hide(currtFragment);
        }
        transaction.commit();
        currtFragment = newFragment;
    }


    /**
     * 改变现象卡的选中状态
     */
    private void clearStatus() {
        //index为上个选中的页面，此方法为使其重新回复为选择状态
        //basicinformation  columndetail  crossing materialstatistics  electrichousehold
        if (index == Constant.basicinformation) {
            tab1.setBackgroundResource(R.drawable.grayline_wihte_tabuncheck_item);
            basicinformation.setTextColor(getResources().getColor(R.color.title));
        } else if (index == Constant.columndetail) {
            tab2.setBackgroundResource(R.drawable.grayline_wihte_tabuncheck_item);
            columndetail.setTextColor(getResources().getColor(R.color.title));
        } else if (index == Constant.crossing) {
            tab3.setBackgroundResource(R.drawable.grayline_wihte_tabuncheck_item);
            crossing.setTextColor(getResources().getColor(R.color.title));
        } else if (index == Constant.materialstatistics) {
            tab4.setBackgroundResource(R.drawable.grayline_wihte_tabuncheck_item);
            materialstatistics.setTextColor(getResources().getColor(R.color.title));
        } else if (index == Constant.electrichousehold) {
            tab5.setBackgroundResource(R.drawable.grayline_wihte_tabuncheck_item);
            electrichousehold.setTextColor(getResources().getColor(R.color.title));
        } else if (index == Constant.problems) {
            tab6.setBackgroundResource(R.drawable.grayline_wihte_tabuncheck_item);
            problems.setTextColor(getResources().getColor(R.color.title));
        }
    }
}
